package client;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class UDPClient {
	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		String request = "PING";				// Message sent to the server.
		String reply;							// Reply received from the server.
		byte[] msgOut = (request).getBytes();	// Message to send in the packet.
		byte[] msgIn = new byte[256];			// Message received in the packet.
		final int serverPort = 8967;			// Server port.
		InetAddress server = null;				// IP Address of server.
		try {
			server = InetAddress.getByName("224.0.2.10");
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
	
		// Sending a request.
		DatagramSocket r = new DatagramSocket();
		DatagramPacket p = new DatagramPacket(msgOut, msgOut.length, server, serverPort);
		r.send(p);
		System.out.println("Message sent: " + request + ".\n");
		
		// Waiting for the reply.
		DatagramPacket pr = new DatagramPacket(msgIn, msgIn.length);
		System.out.println("Waiting for reply...\n");
		r.receive(pr);

		// Process reply
		reply = new String(pr.getData(), 0, pr.getLength());
		System.out.println("Reply received: " + reply + "\n");	
	}
}
